import { BrowserInfo } from '@/api/BrowserInfo.js'
let browserInfo=BrowserInfo()
export const openApp = function(openUrl,IphoneUrl,callback) {
    //检查app是否打开  openUrl 安卓协议地址   IphoneUrl 苹果协议地址    
    function checkOpen(cb){    	
        var _clickTime = +(new Date());
        function check(elsTime) {
            if ( elsTime > 4000 || document.hidden || document.webkitHidden) {
               cb()
            }
        }
        //启动间隔20ms运行的定时器，并检测累计消耗时间是否超过3000ms，超过则结束
        var _count = 0, intHandle;
        intHandle = setInterval(function(){
            _count++;        
            var elsTime = +(new Date()) - _clickTime;
            if (_count>=150 || elsTime > 4000 ) {
                clearInterval(intHandle);
                check(elsTime);
            }
        }, 20);
    }  
    //在iframe 中打开APP
    var ifr = document.createElement('iframe');
    if(browserInfo.isAndroid){
      ifr.src = openUrl;    	
    }
    if(browserInfo.isIphone){ 
      window.location.href=IphoneUrl
    }
    ifr.style.display = 'none';    
    if (callback) {    	  
      checkOpen(callback);  
    }
    document.body.appendChild(ifr);      
    setTimeout(function() {
        document.body.removeChild(ifr);
    }, 2000);  

}